added Feb 2001 SDK
[windows-sources.git] / shared source / sscli20 / jscript / engine / jsfieldmethod.cs
blob077cbc174a7cb1409fe16aa6fe3ae7d8e8b912e9
1 // ==++==
2 //
3 //
4 // Copyright (c) 2006 Microsoft Corporation. All rights reserved.
5 //
6 // The use and distribution terms for this software are contained in the file
7 // named license.txt, which can be found in the root of this distribution.
8 // By using this software in any fashion, you are agreeing to be bound by the
9 // terms of this license.
10 //
11 // You must not remove this notice, or any other, from this software.
12 //
13 //
14 // ==--==
16 namespace Microsoft.JScript {
18 using System;
19 using System.Reflection;
20 using System.Reflection.Emit;
21 using System.Globalization;
22 using System.Diagnostics;
24 internal sealed class JSFieldMethod : JSMethod{
25 internal FieldInfo field;
26 internal FunctionObject func;
28 private static readonly ParameterInfo[] EmptyParams = new ParameterInfo[0];
30 internal JSFieldMethod(FieldInfo field, Object obj)
31 : base(obj){ //The object is never used, but it is convenient to have the field on JSMethod.
32 this.field = field;
33 this.func = null;
34 if (!field.IsLiteral)
35 return;
36 //If we get here, we know at compile time what function we are calling via the field. I.e. we're in fast mode.
37 Object val = field is JSVariableField ? ((JSVariableField)field).value : field.GetValue(null);
38 if (val is FunctionObject)
39 this.func = (FunctionObject)val;
42 internal override Object Construct(Object[] args){
43 return LateBinding.CallValue(this.field.GetValue(this.obj), args, true, false, ((ScriptObject)this.obj).engine, null, JSBinder.ob, null, null);
46 public override MethodAttributes Attributes{
47 get{
48 if (this.func != null)
49 return this.func.attributes;
50 else
51 if (field.IsPublic)
52 return MethodAttributes.Public;
53 else if (field.IsFamily)
54 return MethodAttributes.Family;
55 else if (field.IsAssembly)
56 return MethodAttributes.Assembly;
57 else
58 return MethodAttributes.Private;
62 public override Type DeclaringType{
63 get{
64 if (this.func != null)
65 return Convert.ToType(this.func.enclosing_scope);
66 else
67 return Typeob.Object;
71 internal ScriptObject EnclosingScope(){
72 if (this.func != null)
73 return this.func.enclosing_scope;
74 else
75 return null;
78 public override Object[] GetCustomAttributes(bool inherit){
79 if (this.func != null){
80 CustomAttributeList caList = this.func.customAttributes;
81 if (caList != null) return (Object[])caList.Evaluate(inherit);
83 return new Object[0];
86 public override ParameterInfo[] GetParameters(){
87 if (this.func != null)
88 return this.func.parameter_declarations;
89 else
90 return JSFieldMethod.EmptyParams;
93 internal override MethodInfo GetMethodInfo(CompilerGlobals compilerGlobals){
94 return this.func.GetMethodInfo(compilerGlobals);
97 #if !DEBUG
98 [DebuggerStepThroughAttribute]
99 [DebuggerHiddenAttribute]
100 #endif
101 internal override Object Invoke(Object obj, Object thisob, BindingFlags options, Binder binder, Object[] parameters, CultureInfo culture){
102 bool construct = (options & BindingFlags.CreateInstance) != 0;
103 bool brackets = (options & BindingFlags.GetProperty) != 0 && (options & BindingFlags.InvokeMethod) == 0;
104 Object f = this.func;
105 if (f == null) f = this.field.GetValue(this.obj);
106 FunctionObject func = f as FunctionObject;
107 JSObject jsOb = obj as JSObject;
108 if (jsOb != null && func != null && func.isMethod && (func.attributes & MethodAttributes.Virtual) != 0 &&
109 jsOb.GetParent() != func.enclosing_scope && ((ClassScope)func.enclosing_scope).HasInstance(jsOb)){
110 LateBinding lb = new LateBinding(func.name);
111 lb.obj = jsOb;
112 return lb.Call(parameters, construct, brackets, ((ScriptObject)this.obj).engine);
114 return LateBinding.CallValue(f, parameters, construct, brackets, ((ScriptObject)this.obj).engine, thisob, binder, culture, null);
117 internal bool IsAccessibleFrom(ScriptObject scope){
118 return ((JSMemberField)this.field).IsAccessibleFrom(scope);
121 public override String Name{
122 get{
123 return this.field.Name;
127 public override Type ReturnType{
128 get{
129 if (this.func != null)
130 return Convert.ToType(this.func.ReturnType(null));
131 else
132 return Typeob.Object;
136 internal IReflect ReturnIR(){
137 if (this.func != null)
138 return this.func.ReturnType(null);
139 else
140 return Typeob.Object;